package com.panshi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;



import com.panshi.bean.BeiUser;

import com.panshi.api.vo.UserLoginVo;
import com.panshi.common.EmailPhoneUtill;
import com.panshi.mapper.BeiUserMapper;
import com.panshi.service.BeiUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import java.util.Date;

/**
 * <p>
 * 用户表  服务实现类
 * </p>
 *
 * @author 小伍
 * @since 2020-08-01
 */
@Service
public class BeiUserServiceImpl  implements BeiUserService {

    @Autowired
    private BeiUserMapper beiUserMapper;

    @Autowired
    private EmailPhoneUtill emailPhoneUtill;

    @Override
    public BeiUser queryUserByuid(String uid) {
        return beiUserMapper.selectById(uid);
    }

    @Override
    public BeiUser queryUserBype(UserLoginVo beiUser) {
        System.out.println("我进来了");
        System.out.println(beiUser);
         QueryWrapper<BeiUser> queryWrapper=new QueryWrapper<>();
         if(StringUtils.isEmpty(beiUser.getIphone())){
             queryWrapper.eq("email", beiUser.getEmail());
         }else{
             queryWrapper.eq("iphone", beiUser.getIphone());
         }
        return beiUserMapper.selectOne(queryWrapper);
    }

    @Override
    public void addUser(UserLoginVo beiUser) {
        BeiUser beiUser1 = new BeiUser();
        if(StringUtils.isEmpty(beiUser.getIphone())){
            beiUser1.setName(beiUser.getEmail().replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "$1****$3$4"));
            beiUser1.setUsername(beiUser.getEmail().replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "$1****$3$4"));
        }else{
            beiUser1.setName( beiUser.getIphone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
            beiUser1.setUsername(beiUser.getIphone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
        }
        BeanUtils.copyProperties(beiUser,beiUser1);
        System.out.println("===============================");
        System.out.println(beiUser1);
        beiUser1.setCreateTime(new Date());
        beiUser1.setState("1");
        beiUserMapper.insert(beiUser1);
    }

    @Override
    public void updateUser(BeiUser beiUser) {
        beiUserMapper.updateById(beiUser);
    }

    @Override//0上架 1 删除 2下架
    public void setemailarticle(Integer uid,Integer state,String title) throws MessagingException {
        BeiUser beiUser = beiUserMapper.selectById(uid);//查询用户
        if(StringUtils.isEmpty(beiUser.getEmail())){
            return ;
        }
        String message="";
        if(state==0){
            message ="<p style='color:red'>你好</p><p>恭喜你 你在渣男口腔网发布的文章:</p>《"+title+"》<p>审核通过 已上架</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }else if(state==2){
            message ="<p style='color:red'>你好</p><p>很遗憾 你在渣男口腔网发布的文章:</p>《"+title+"》<p>内容违反国家法律规定 已被强制下架 请修改内容后重新发布</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }
    }

    @Override//状态(2通过 3未通过)
    public void setemailarticleno(Integer uid, Integer state, String title) throws MessagingException {
        BeiUser beiUser = beiUserMapper.selectById(uid);//查询用户
        if(StringUtils.isEmpty(beiUser.getEmail())){
            return ;
        }
        String message="";
        if(state==2){
            message ="<p style='color:red'>你好</p><p>恭喜你 你在渣男口腔网发布的文章:</p>《"+title+"》<p>审核通过</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }else if(state==3){
            message ="<p style='color:red'>你好</p><p>很遗憾 你在渣男口腔网发布的文章:</p>《"+title+"》<p>内容违反国家法律规定 审核为成功 请修改内容后重新发布</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }
    }

    @Override
    public void setemailquestion(Integer uid, Integer state, String title) throws MessagingException {
        BeiUser beiUser = beiUserMapper.selectById(uid);//查询用户
        if(StringUtils.isEmpty(beiUser.getEmail())){
            return ;
        }
        String message="";
        if(state==1){
            message ="<p style='color:red'>你好</p><p>恭喜你 你在渣男口腔网发布的问题:</p>《"+title+"》<p>审核通过 已上架</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }else if(state==2){
            message ="<p style='color:red'>你好</p><p>很遗憾 你在渣男口腔网发布的文章:</p>《"+title+"》<p>内容违反国家法律规定 已被强制下架 请修改内容后重新发布</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }
    }

    @Override
    public void setemailquestionno(Integer uid, Integer state, String title) throws MessagingException {
        BeiUser beiUser = beiUserMapper.selectById(uid);//查询用户
        if(StringUtils.isEmpty(beiUser.getEmail())){
            return ;
        }
        String message="";
        if(state==2){
            message ="<p style='color:red'>你好</p><p>很遗憾 你在渣男口腔网提出的问题:</p>《"+title+"》<p>违反国家法律规定 审核不通过 请修改内容后重新发布</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }else if(state==3){
            message ="<p style='color:red'>你好</p><p>恭喜你 你在渣男口腔网提出的问题:</p>《"+title+"》<p>审核通过</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }
    }

    @Override
    public void setemailanswer(Integer uid, Integer state, String title) throws MessagingException {
        BeiUser beiUser = beiUserMapper.selectById(uid);//查询用户
        if(StringUtils.isEmpty(beiUser.getEmail())){
            return ;
        }
        String message="";
        if(state==1){
            message ="<p style='color:red'>你好</p><p>恭喜你 你在渣男口腔网回答的内容:</p>《"+title+"》<p>审核通过 已上架</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }else if(state==2){
            message ="<p style='color:red'>你好</p><p>很遗憾 你在渣男口腔网回答的内容:</p>《"+title+"》<p>违反国家法律规定 已被强制下架 请修改内容后重新发布</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }
    }

    @Override
    public void setemailanswerno(Integer uid, Integer state, String title) throws MessagingException {
        BeiUser beiUser = beiUserMapper.selectById(uid);//查询用户
        if(StringUtils.isEmpty(beiUser.getEmail())){
            return ;
        }
        String message="";
        if(state==2){
            message ="<p style='color:red'>你好</p><p>很遗憾 你在渣男口腔网回答的内容:</p>《"+title+"》<p>违反国家法律规定 审核不通过 请修改内容后重新发布</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }else if(state==3){
            message ="<p style='color:red'>你好</p><p>恭喜你 你在渣男口腔网回答的内容:</p>《"+title+"》<p>审核通过</p>";
            emailPhoneUtill.setemailarticle(beiUser.getEmail(),message);
        }
    }


}
